package com.gxa.service.impl;
import com.alibaba.fastjson.JSON;
import com.gxa.common.R;
import com.gxa.mapper.ContactMapper;
import com.gxa.pojo.Contact;
import com.gxa.service.ContactService;
import com.gxa.utils.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class ContactServiceImpl implements ContactService{
    @Autowired(required = false)
    private ContactMapper contactMapper;
    //新增联系人
    @Override
    public R addContact(Contact contact) {
        R r = new R();
        contactMapper.addContact(contact);
        r.setCode(200);
        r.setMsg("新增成功");
        return r;
    }

    @Override
    public R queryContact(Integer customer_id) {
        R r = new R();
        List<Contact> list = contactMapper.queryContact(customer_id);
        r.setCode(200);
        r.setMsg("查询成功");
        r.setData(list);
        return r;
    }

    @Override
    public R delContact(Integer contact_id) {
        R r = new R();
        contactMapper.delContact(contact_id);
        r.setCode(200);
        r.setMsg("删除成功");
        return r;
    }

    @Override
    public R upDataContact(Contact contact) {
        R r = new R();
        Integer id = contact.getContact_id();
        String str = JSON.toJSONString(contact);
        Map map = MapUtils.delNullMap((Map)JSON.parse(str));
        map.keySet().removeIf(key -> key.equals("contact_id"));
        contactMapper.upDataContact(map,id);
        r.setCode(200);
        r.setMsg("修改成功");
        return r;
    }

    @Override
    public R delCustomerContact(Integer customer_id) {
        R r = new R();
        contactMapper.delCustomerContact(customer_id);
        r.setCode(200);
        r.setMsg("删除成功");
        return r;
    }
}
